﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Icoders.MarketTracker.Core.MasterData;
using Icoders.MarketTracker.Core.Repository.MasterDataRepositories;
using Icoders.MarketTracker.Core.Validation;
using Icoders.MarketTracker.Web.Lib.Security;
using Icoders.MarketTracker.Web.Lib.Services;
using Icoders.MarketTracker.Web.Lib.Validation;

namespace Icoders.MarketTracker.Web.Areas.AdminFRS.Controllers
{
    [CustomAuthorize(Roles = "ROLE_FSRADMIN")]
    public class FrsAssignController : Controller
    {
        private IFrsAssignRepository _frsassignRepository;
        private IUserRepository _userRepository;
        private IDropDownService _dropDownService;
        private IAuditTrailRepository _auditTrailRepository;

        public FrsAssignController(IFrsAssignRepository frsassignRepository, IUserRepository userRepository, IDropDownService dropDownService, IAuditTrailRepository auditTrailRepository)
        {
            _frsassignRepository = frsassignRepository;
            _userRepository = userRepository;
            _dropDownService = dropDownService;
            _auditTrailRepository = auditTrailRepository;
        }

        private void Bind()
        {
            ViewBag.CountryList = _dropDownService.GetCountry();
            ViewBag.CityList = _dropDownService.GetAllCity();
            ViewBag.ClientList = _dropDownService.GetSupplier();
            ViewBag.AreaList = _dropDownService.GetAllArea();
            ViewBag.ZoneList = _dropDownService.GetAllZone();
        }
        public ActionResult Index()
        {
            return View(_userRepository.GetByType(UserType.MobileAppUser));
        }
        public ActionResult Assign(int user)
        {
            FrsAssign frsassign = new FrsAssign();
            frsassign.UserId = user;
            UserDetails(user);
            Bind();
            return View(frsassign);
        }

        private void UserDetails(int user)
        {
            var usr = _userRepository.GetById(user);
            ViewBag.UserName ="Username: "+ usr.Username+" Full names :"+ usr.FirstName+" "+usr.LastName;
        }

        [HttpPost]
        public ActionResult Assign(FrsAssign model)
        {
            try
            {
                _frsassignRepository.Save(model);
                Saveaudit("Added FSR", HttpContext.User.Identity.Name, ActionType.Added);
            }
            catch (DomainValidationException dve)
            {
                Bind();
                UserDetails(model.UserId);
                ValidationSummary.DisplayDomainValidationResult(dve, ModelState);
                return View(model);
            }
            catch (Exception ex)
            {
                Bind();
                UserDetails(model.UserId);
                ValidationSummary.DisplayValidationResult(ex.Message, ModelState);
                return View(model);
            }
            return RedirectToAction("Index");
        }
        public ActionResult Cities(int countyId)
        {
            var item = _dropDownService.GetCity(countyId);
            return Json(item, JsonRequestBehavior.AllowGet);
        }
        public ActionResult Areas(int cityId)
        {
            var item = _dropDownService.GetArea(cityId);
            return Json(item, JsonRequestBehavior.AllowGet);
        }
        public ActionResult Zones(int areaId)
        {
            var item = _dropDownService.GetZone(areaId);
            return Json(item, JsonRequestBehavior.AllowGet);
        }
        public ActionResult Details(int userid)
        {
            FrsAssign frsassign = new FrsAssign();
            frsassign = _frsassignRepository.GetByUser(userid);
           
            return View(frsassign);
        }
        public ActionResult Delete(int userid)
        {
            _frsassignRepository.Delete(userid);
            Saveaudit("Deleted FSR", HttpContext.User.Identity.Name, ActionType.Deleted);
            return RedirectToAction("index");
        }
        public void Saveaudit(string description, string username, ActionType actionType)
        {
            //  private IUserRepository _userRepository;
            //private IAuditTrailRepository _auditTrailRepository;
            // Saveaudit("Changed Password", HttpContext.User.Identity.Name, ActionType.Updated);
            try
            {
                User user = _userRepository.GetByUsername(username);
                AuditTrail auditTrail = new AuditTrail();
                auditTrail.Description = description;
                auditTrail.UserId = user.Id;
                auditTrail.ActionTypeId = Convert.ToInt32(actionType);
                _auditTrailRepository.Save(auditTrail);
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}
